package com.group12.liuxinpeng.user.service.impl;

import com.group12.liuxinpeng.login.entity.MenuLevelEntity;
import com.group12.liuxinpeng.mybatis.entity.PtMenuEntity;
import com.group12.liuxinpeng.mybatis.entity.PtUserEntity;
import com.group12.liuxinpeng.mybatis.flexigrid.PadingRstType;
import com.group12.liuxinpeng.mybatis.flexigrid.PagingBean;
import com.group12.liuxinpeng.mybatis.ztree.ZtreeEntity;
import com.group12.liuxinpeng.user.entity.UserSearchEntity;
import com.group12.liuxinpeng.user.repository.UserDao;
import com.group12.liuxinpeng.user.repository.UserInfoDao;
import com.group12.liuxinpeng.user.service.UserService;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final static Integer MENU_FIRST_LEVEL = 1;
    @Resource
    private UserDao userDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private MessageSource messageSource;


    @Override
    public PtUserEntity queryUserInfoByUserName(String username) {
        return userDao.queryUserInfoByUserName(username);
    }

    @Override
    public List<MenuLevelEntity> queryMenuInfoByLang(String lang) {
        List<MenuLevelEntity> levelEntities = new ArrayList<>();
        List<PtMenuEntity> ptMenuEntities = userDao.queryMenuInfoByLang(lang);
        MenuLevelEntity menuLevelEntity1 = null;
        MenuLevelEntity menuLevelEntity2 = null;
        MenuLevelEntity menuLevelEntity3 = null;
        for (PtMenuEntity ptMenuEntity1 : ptMenuEntities) {
            if (MENU_FIRST_LEVEL.equals(ptMenuEntity1.getMenuLevel())) {
                menuLevelEntity1 = new MenuLevelEntity(ptMenuEntity1);
                levelEntities.add(menuLevelEntity1);
                for (PtMenuEntity ptMenuEntity2 : ptMenuEntities) {
                    if (ptMenuEntity1.getMenuId().equals(ptMenuEntity2.getParentId())) {
                        menuLevelEntity2 = new MenuLevelEntity(ptMenuEntity2);
                        menuLevelEntity1.addList(menuLevelEntity2);
                        for (PtMenuEntity ptMenuEntity3 : ptMenuEntities) {
                            if (ptMenuEntity2.getMenuId().equals(ptMenuEntity3.getParentId())) {
                                menuLevelEntity3 = new MenuLevelEntity(ptMenuEntity3);
                                menuLevelEntity2.addList(menuLevelEntity3);
                            }
                        }
                    }
                }
            }
        }
        return levelEntities;
    }

    @Override
    public PadingRstType<PtUserEntity> getUserInfoListByPage(UserSearchEntity search, PagingBean paging) {
        paging.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtUserEntity> list = userDao.queryUserListByPadding(search,paging);
        Integer total = userDao.queryUserListTotal(search);
        padingRstType.setRawRecords(list);
        padingRstType.setTotal(total);
        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public void deleteUserById(String userUuid) {
        userDao.deleteUserById(userUuid);
    }

    @Override
    public List<ZtreeEntity> getRoleZtreeListByUserId(String userUuid) {
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage("user.role.info",null,locale);
        List<ZtreeEntity> list=userDao.getRoleZtreeList();
        ZtreeEntity root=new ZtreeEntity();
        root.setId("0");
        root.setpId("-1");
        root.setName(msgStr);
        list.add(root);
        return list;
    }

    @Override
    public List<ZtreeEntity> getRoleZtreeListByUserId1(String userUuid) {
        List<ZtreeEntity> ztreeEntityList = userDao.getRoleZtreeList();
        List<String> selectRoleIdList = userDao.queryRoleZtreeListByUserId(userUuid);
        for(ZtreeEntity roleEntity:ztreeEntityList) {
            for(String roleId:selectRoleIdList)
            {
                if(roleId.equals(roleEntity.getId())){
                    roleEntity.setChecked(true);
                    break;
                }
            }
        }
        return ztreeEntityList;
    }

    @Override
    public void allocateRoleByUser(String userUuid, String roleArray) {
        userDao.delRoleByUserId(userUuid);
        String[] roles = roleArray.split(",");
        for(String roleId:roles){
            userDao.insertRoleRefUser(roleId,userUuid);
        }
    }
}
